package com.movies.service.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.movies.service.common.Constants;
import com.movies.service.common.Result;
import com.movies.service.entity.Ticket;
import com.movies.service.service.TicketService;
import com.movies.service.mapper.TicketMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/admin/ticket")
@SaCheckLogin
@SaCheckRole("ADMIN")
public class AdminTicketController {

    @Autowired
    private TicketService ticketService; // 仅分页
    @Autowired
    private TicketMapper ticketMapper;   // 原生 SQL CRUD

//    分页查询订单列表（支持按状态/用户名/电影名筛选）
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNo,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) String status,
                       @RequestParam(required = false) String uname,
                       @RequestParam(required = false) String mname) {
        LambdaQueryWrapper<Ticket> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(status != null && !status.isEmpty(), Ticket::getStatus, status)
                .like(uname != null && !uname.isEmpty(), Ticket::getUname, uname)
                .like(mname != null && !mname.isEmpty(), Ticket::getMname, mname);
        Page<Ticket> page = ticketService.page(new Page<>(pageNo, pageSize), wrapper);
        return Result.success("查询成功", page);
    }

//    根据ID获取订单详情
    @GetMapping("/{id}")
    public Result get(@PathVariable Long id) {
        return Result.success(ticketMapper.selectTicketById(id));
    }

//    新增订单
    @PostMapping("/create")
    public Result create(@RequestBody Ticket ticket) {
        int rows = ticketMapper.insertTicket(ticket);
        return rows > 0 ? Result.success("新增成功") : Result.error(Constants.CODE_600, "新增失败");
    }

//    更新订单信息
    @PutMapping("/update")
    public Result update(@RequestBody Ticket ticket) {
        int rows = ticketMapper.updateTicketById(ticket);
        return rows > 0 ? Result.success("更新成功") : Result.error(Constants.CODE_600, "更新失败");
    }

//    删除订单
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        int rows = ticketMapper.deleteTicketById(id);
        return rows > 0 ? Result.success("删除成功") : Result.error(Constants.CODE_600, "删除失败");
    }

//    批量删除订单
    @DeleteMapping("/batch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        int rows = ticketMapper.deleteTicketsByIds(ids);
        return rows > 0 ? Result.success("批量删除成功") : Result.error(Constants.CODE_600, "批量删除失败");
    }
}
